'use strict';
/**
 * @ngdoc overview
 * @name sbAdminApp
 * @description
 * # sbAdminApp
 *
 * Main module of the application.
 */
angular
    .module('sbAdminApp', [
        'oc.lazyLoad',
        'ui.router',
        'ui.bootstrap',
        'angular-loading-bar',
        'ngResource',
        'ngAnimate',
        'ncy-angular-breadcrumb',
        'angularBootstrapNavTree',
        'ngMessages',
        'mk.controllers',
        'mk.services'
    ])
    /**
     * angularJs启动的时候只会执行一次，这里将state、stateParams放入rootScope中，目的是为了在
     * 后面可以方便的引用和注入
     */
    // 初始化全局变量设置
//    .constant('Settings', {
//        Context: {
//            path: ''
//        }
//    })
    .constant('contextpath', '/')
    .constant('ITEMPERSIZE', 10)
    .config(['$stateProvider', '$urlRouterProvider', '$ocLazyLoadProvider', '$breadcrumbProvider', '$httpProvider', function ($stateProvider, $urlRouterProvider, $ocLazyLoadProvider, $breadcrumbProvider, $httpProvider) {

        // 使angular $http post提交和jQuery一致
        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
        // Override $http service's default transformRequest
        $httpProvider.defaults.transformRequest = [function (data) {
            /**
             * The workhorse; converts an object to x-www-form-urlencoded serialization.
             * @param {Object} obj
             * @return {String}
             */
            var param = function (obj) {
                var query = '';
                var name, value, fullSubName, subName, subValue, innerObj, i;

                for (name in obj) {
                    value = obj[name];

                    if (value instanceof Array) {
                        for (i = 0; i < value.length; ++i) {
                            subValue = value[i];
                            fullSubName = name + '[' + i + ']';
                            innerObj = {};
                            innerObj[fullSubName] = subValue;
                            query += param(innerObj) + '&';
                        }
                    } else if (value instanceof Object) {
                        for (subName in value) {
                            subValue = value[subName];
                            fullSubName = name + '[' + subName + ']';
                            innerObj = {};
                            innerObj[fullSubName] = subValue;
                            query += param(innerObj) + '&';
                        }
                    } else if (value !== undefined && value !== null) {
                        query += encodeURIComponent(name) + '='
                            + encodeURIComponent(value) + '&';
                    }
                }

                return query.length ? query.substr(0, query.length - 1) : query;
            };

            return angular.isObject(data) && String(data) !== '[object File]'
                ? param(data)
                : data;
        }];

//        $breadcrumbProvider.setOptions({
//            templateUrl: 'scripts/directives/breadcrumb/breadcrumb.html'
//        });

        $ocLazyLoadProvider.config({
            debug: false,
            events: true
        });

        //进入自动请求地址
        $urlRouterProvider.otherwise('/mk/init');

        $stateProvider
            .state('mk', {
                url: '/mk',
                templateUrl: 'views/dashboard/main.html',
                ncyBreadcrumb: {
                    label: '首页'
                },
                resolve: {
                    loadMyDirectives: function ($ocLazyLoad) {
                        return $ocLazyLoad.load(
                            {
                                name: 'sbAdminApp',
                                files: [
                                    'scripts/directives/header/header.js',
                                    'scripts/directives/sidebar/sidebar.js',
                                ]
                            });
                    }
                }
            })
            .state('mk.init', {
                url: '/init',
                controller: 'MainCtrl',
                templateUrl: 'views/dashboard/home.html',
                ncyBreadcrumb: {
                    label: '欢迎'
                },
                resolve: {
                    loadMyFiles: function ($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            name: 'sbAdminApp',
                            files: [
                                'scripts/controllers/main.js',
                            ]
                        });
                    }
                }
            })
            //
            .state('mk.order', {
                url: '/order',
                templateUrl: '',
                ncyBreadcrumb: {
                    label: '订单纬度统计'
                }
            })
            //订单
            .state('mk.order.statistica', {
                url: '/statistica',
                controller: 'StatisticaorderCtrl',
                templateUrl: 'views/service/statisticaorder/list.html',
                resolve: {
                    loadMyFiles: function ($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            name: 'sbAdminApp',
                            files: [
                                'scripts/controllers/statisticaorderController.js',
                                'scripts/services/statisticaorderService.js',
                                'scripts/directives/FileSaver.js'
                            ]
                        });
                    }
                },
                ncyBreadcrumb: {
                    label: '订单纬度'
                }
            })
           .state('mk.result', {
                url: '/result',
                templateUrl: '',
                ncyBreadcrumb: {
                    label: '结算结果统计'
                }
            })
            //结算数据
            .state('mk.result.settlement', {
                url: '/settlement',
                //cache:true,
                controller: 'SettlementresultCtrl',
                templateUrl: 'views/service/settlementresult/list.html',
                resolve: {
                    loadMyFiles: function ($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            name: 'sbAdminApp',
                            files: [
                                'scripts/controllers/settlementresultController.js',
                                'scripts/services/settlementresultService.js',
                                'scripts/directives/FileSaver.js'
                            ]
                        });
                    }
                },
                ncyBreadcrumb: {
                    label: '结算统计'
                }
            })
            .state('mk.task', {
                url: '/task',
                templateUrl: '',
                ncyBreadcrumb: {
                    label: '定时调度'
                }
            })
            .state('mk.task.scheduler', {
                url: '/scheduler',
                controller: 'SettlementSchedulerCenterCtrl',
                templateUrl: 'views/service/scheduler/list.html',
//                resolve: {
//                    loadController: function ($ocLazyLoad) {
//                        return $ocLazyLoad.load({
//                            name: 'mk.controllers',
//                            files: ['scripts/controllers/settlementSchedulerCenterController.js']
//                        });
//                    },
//                    loadService: function($ocLazyLoad) {
//                        return $ocLazyLoad.load({
//                            name: 'mk.services',
//                            files: ['scripts/services/settlementSchedulerCenterService.js']
//                        });
//                    }
//                },
                
                resolve: {
                    loadMyFiles: function ($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            name: 'sbAdminApp',
                            files: [
                                'scripts/controllers/settlementSchedulerCenterController.js',
                                'scripts/services/settlementSchedulerCenterService.js'
                            ]
                        });
                    }
                },
                ncyBreadcrumb: {
                    label: '任务调度中心'
                }
            })
            
            
            .state('mk.task.list', {
                url: '/list',
                controller: 'TaskManagerCtrl',
                templateUrl: 'views/task/list.html',
                resolve: {
                    loadController: function ($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            name: 'mk.controllers',
                            files: ['scripts/controllers/TaskManagerCtrl.js']
                        });
                    },
                    loadService: function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            name: 'mk.services',
                            files: ['scripts/services/TaskService.js']
                        });
                    }
                },
                ncyBreadcrumb: {
                    label: '定时调度任务列表'
                }
            })
            .state('mk.task.card', {
                url: '/card',
                controller: 'TaskManagerCtrl',
                templateUrl: 'views/task/card.html',
                resolve: {
                    loadMyFiles: function ($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            name: 'sbAdminApp',
                            files: ['scripts/controllers/TaskManagerCtrl.js']
                        });
                    }
                },
                ncyBreadcrumb: {
                    label: '定时调度任务信息'
                }
            });
    }]);

    
